//
//  SwiftUIAnimation.swift
//  SwiftUICode
//
//  Created by yangkl on 2024/2/26.
//

import SwiftUI

struct SwiftUIAnimation: View {
    
    
    @State private var circleColorChanged = false
    
    @State private var heartColorChanged = false
    
    @State private var heartSizeChanged = false
    
    var body: some View {
        ZStack{
            Circle()
                .frame(width: 200, height: 200)
                .foregroundColor(circleColorChanged ?  Color(.systemGray5) : .red)
//                .animation(.default, value: circleColorChanged)
            
            Image(systemName: "heart.fill")
                .foregroundColor(heartColorChanged ? .red : .white)
                .font(.system(size: 100))
                .scaleEffect(heartSizeChanged ? 1.0 : 0.5)
//                .animation(.easeIn, value: heartSizeChanged)
        }
//        .animation(.default, value: circleColorChanged)
//        .animation(.spring(response: 0.3, dampingFraction: 0.3, blendDuration: 0.3), value: heartSizeChanged)
        .onTapGesture {
            withAnimation(.spring(response: 0.3, dampingFraction: 0.3, blendDuration: 0.5)){
                circleColorChanged.toggle()
                heartColorChanged.toggle()
                heartSizeChanged.toggle()
                
            }
            
            
        }
    }
}


struct SwiftUIMedium: View {
    @State private var isLoading = false
    var body: some View {
        ZStack{
            Circle()
                .stroke(Color(.systemGray5), lineWidth: 14)
                .frame(width: 100, height: 100)
            Circle()
                .trim(from: 0, to: 0.3)
                .stroke(Color.green, lineWidth: 7)
                .frame(width: 100, height: 100)
                .rotationEffect(Angle(degrees: isLoading ? 360 : 0))
                .animation(.linear(duration: 1).repeatForever(autoreverses: false), value: isLoading)
                .onAppear(){
                    isLoading = true
                }
        }
        
    }
}

struct SwiftUILoading: View {
    @State private var isLoading = false
    var body: some View {
        ZStack{
            RoundedRectangle(cornerRadius: 30)
                .fill(Color(.systemGray5))
                .frame(width: 390, height: 20)
            
            RoundedRectangle(cornerRadius: 30)
                .fill(.green)
                .offset(x:-100, y: 0)
                .frame(width: 30, height: 10)
                
        }
        
    }
}
#Preview {
    SwiftUIAnimation()
    
}

#Preview{
    SwiftUIMedium()
}


#Preview{
    SwiftUILoading()
}
